package controller
{
	import com.tb.sng.controller.BaseController;
	import com.tb.sng.utils.debug.Debug;
	import com.tb.sng.utils.loader.HttpLoader;
	
	import config.UrlConfig;
	
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.system.Capabilities;
	
	import view.Dialog;
	import view.RegView;
	import view.WheelView;
	
	public class MainController extends BaseController
	{
		private var wheelView:WheelView;
		private var regView:RegView;
		
		public function MainController()
		{
			super();
		}
		
		public static function getInstance():MainController
		{
			return BaseController.getInstance(MainController) as MainController;
		}
		
		public function init():void
		{
			if (wheelView == null){
				wheelView = new WheelView();
				wheelView.setContainer(this);
				Application.getInstance().mainMc.addChild(wheelView);
			}
			
			if (Capabilities.playerType == "StandAlone"){
				wheelView.setInfo(null);
				loginAction("rendong", "rendong");
			}else
				getInfo();
			
			trace("init");
		}
		
		/**
		 * 获取用户信息 
		 * 
		 */		
		private function getInfo():void
		{
			
			HttpLoader.httpJson(UrlConfig.HTTPROOT + "index.php?a=info", function (result:Object):void{
				wheelView.setInfo(result);
			});
		}
		
		/**
		 * 登录 
		 * @param username
		 * @param password
		 * 
		 */		
		public function loginAction(username:String, password:String):void
		{
			var vars:URLVariables = new URLVariables();
			vars.username = username;
			vars.password = password;
			
			HttpLoader.httpJson(UrlConfig.HTTPROOT + "index.php?a=dologin", function (result:Object):void{
				if (result['status'] == 1){
					getInfo();
				}else{
					Dialog.show(result['message']);
				}
			}, vars, URLRequestMethod.POST);
		}
		
		/**
		 * 下注 
		 * @param vars
		 * 
		 */		
		public function betAction(vars:URLVariables):void
		{
			HttpLoader.httpJson(UrlConfig.HTTPROOT + "index.php?a=dobet", function (result:Object):void{
				if (result['status'] == 1){
					wheelView.setResult(result);
				}else{
					wheelView.setResult(null);
					Dialog.show(result['message']);
				}
			}, vars, URLRequestMethod.POST);
		}
		
		
		/**
		 *退出登录 
		 * 
		 */		
		public function logoutAction():void
		{
			HttpLoader.httpJson(UrlConfig.HTTPROOT + "index.php?a=logout", function (result:Object):void{});
			
			wheelView.setInfo(null);
		}
		
		public function regAction():void
		{
			if (regView == null){
				regView = new RegView();
				regView.setContainer(this);
				Application.getInstance().mainMc.addChild(regView);
				
				Application.getInstance().mainMc.stage.focus = regView.mc.txt_username;
			}
		}
		
		public function closeAction():void
		{
			Application.getInstance().mainMc.removeChild(regView);
			regView.setContainer(null);
			regView = null;
		}
		
		public function doregAction(username:String, password:String):void
		{
			var vars:URLVariables = new URLVariables();
			vars.username = username;
			vars.password = password;
			
			HttpLoader.httpJson(UrlConfig.HTTPROOT + "index.php?a=doreg", function (result:Object):void{
				if (result['status'] == 1){
					getInfo();
					closeAction();
				}else{
					wheelView.setResult(null);
					Dialog.show(result['message']);
				}
			}, vars, URLRequestMethod.POST);
		}
	}
}